<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形解构 -->
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <template #default="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDepartment(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDepartment(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delPartment(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 新增弹窗 组件-->
    <add-department
      ref="addDepart"
      :show-dialog="dialogVisible"
      :cur-id="curId"
      @close-dialog="closeDialog"
      @reset-data="resetData"
    />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/department'
import transTree from '@/utils/transTree'
import addDepartment from './components/add-Department.vue'

export default {
  name: 'DepartmentIndex',
  components: { addDepartment },
  data() {
    return {
      // 数据列表
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 弹窗显示与隐藏
      dialogVisible: false,
      // 子部门id
      curId: ''
    }
  },
  mounted() {
    this.fetchDepartments()
  },

  methods: {
    // 处理树形数据
    async fetchDepartments() {
      // 发送接口请求
      const { depts } = await getDepartments()
      // 问题：后端返回的是扁平的数组 但是我们组件需要的是树形的结构
      // 数据处理  扁平化 -> 树形结构  先处理再赋值
      this.list = transTree(depts)
    },
    // 显示新增弹窗
    addDepartment(id) {
      // console.log(id)
      this.dialogVisible = true
      this.curId = id
    },
    // 关闭弹框
    closeDialog() {
      this.dialogVisible = false
    },
    // 重置
    resetData() {
      // 1. 更新列表
      // 2. 更新curId
      this.fetchDepartments()
      this.curId = ''
    },
    // 删除部门
    delPartment(id) {
      // console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
      // 调接口
        await delDepartments(id)
        // 提示
        this.$message.success('删除成功')
        // 从新渲染树形列表
        this.fetchDepartments()
      }).catch(() => {})
    },
    // 编辑部门
    editDepartment(id) {
      // 打开弹窗
      this.dialogVisible = true
      // 调接口 数据回填
      // 直接调用子组件中的getDetail方法
      // // 并且传递id参数
      this.$refs.addDepart.getDetail(id)
    }
  }

}

</script>

<style scoped>

</style>
